/*
 * Copyright © OpenAtom Foundation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.msu.server;

import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.msu.api.entity.ServiceUnitInfo;
import io.iec.edp.caf.msu.api.entity.ServiceUnitRegisterInfo;
import io.iec.edp.caf.msu.api.server.MsuServiceRegistry;
import io.iec.edp.caf.msu.server.storage.MsuStorage;
import io.iec.edp.caf.msu.server.storage.database.domain.entity.GspMsuRegisterInfo;
import io.iec.edp.caf.tenancy.api.context.MultiTenantContextInfo;
import io.iec.edp.caf.tenancy.core.context.MultiTenantContextHolder;
import lombok.var;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author Leon Huo
 */

public class MsuServiceRegistryImpl implements MsuServiceRegistry {
    private MsuStorage storage;

    public MsuServiceRegistryImpl(MsuStorage storage) {
        this.storage = storage;
    }

    /**
     * 注册应用的su到msuregisterinfo表
     * @param info
     * @return
     */
    @Override
    public Boolean register(ServiceUnitRegisterInfo info) {
        String app = info.getAppName();
        List<ServiceUnitInfo> suList = info.getServiceUnitNameList();
        String service = info.getServiceName();

        var storageInfoList = new ArrayList<GspMsuRegisterInfo>();
        for (var su : suList) {
            var serviceNameList = this.storage.getMsuServiceList(app, su.getName());
            if (serviceNameList.size() == 0 || !serviceNameList.contains(service)) {
                var storageInfo = new GspMsuRegisterInfo();
                storageInfo.setId(UUID.randomUUID().toString());
                storageInfo.setAppName(app);
                storageInfo.setServiceUnitName(su.getName().toLowerCase());
                storageInfo.setServiceName(service);
                storageInfo.setServiceUnitDes(su.getServiceUnitDes());
                storageInfoList.add(storageInfo);
            }
        }

        try{

            //msu的注册默认注册到主库
            MultiTenantContextInfo contextInfo = new MultiTenantContextInfo();
            contextInfo.setMasterDb(true);
            MultiTenantContextHolder.set(contextInfo);
            this.storage.save(storageInfoList);
        }finally {
            MultiTenantContextHolder.set(null);
        }

        return true;
    }

    /**
     * 从msuregisterinfo表注销su
     * @param info
     * @return
     */
    @Override
    public Boolean unRegister(ServiceUnitRegisterInfo info) {
        String app = info.getAppName();
        var suList = new ArrayList<String>();
        info.getServiceUnitNameList().forEach(x->{
            suList.add(x.getName().toLowerCase());
        });
//        List<String> suList = info.getServiceUnitNameList();

        try{

            //msu的注册默认注册到主库
            MultiTenantContextInfo contextInfo = new MultiTenantContextInfo();
            contextInfo.setMasterDb(true);
            MultiTenantContextHolder.set(contextInfo);
            this.storage.deleteByAppAndSu(app,suList);
        }finally {
            MultiTenantContextHolder.set(null);
        }

        return true;
    }


}
